import $service from '../../../../service';
const app = getApp();
let doctorImId = '';
let logId = '';
let inquiryPatientId = '';
let that;
let doctorId;
let log_type;
Page({
 txTic: null,
  /**
   * 页面的初始数据
   */
  data: {
    log_type:"",
    logList: [],
    doctorInfo: {}
  },
onLoad: function(e) {
      doctorImId = e.doctorImId;
      logId = e.logId;
      doctorId = e.doctorId;
      log_type = e.log_type;
      wx.hideHomeButton()
  },
  onShow: function () {
    that = this.data;
    this.getQueuingInfo();
    this.getDoctorDetail();
  },
  // 页面卸载
  onUnload() {
    // 页面意外退出会取消排队
    app.webSocket.sendWebSocketMsg({
      data: {
        "logId": logId,
        "packageType": 11,
      },
    })
  },
  getDoctorDetail() {
    app.http.getDoctorDetail({id: doctorId}).then(res => {
        this.setData({
          doctorInfo: res.data
        })
    })
  },
  //取消排队
  cancel() {
      app.http.unSuccessfulInquiry({id: logId, type: 0}).then(res => {
        app.webSocket.sendWebSocketMsg({
          data: {
            "logId": logId,
            "packageType": 11,
          },
          success: (res) => {
            console.log(res)
            wx.reLaunch({
              url: '/pages/user_index/user_index'
            })
          }
        })
      }).catch(err => {
        $service.showToast(err.msg)
      })
  },
  // 获取排队列表
  getQueuingInfo() {
    if (!app.webSocket.init) {
      app.webSocket.initWebSocket({ url: $service.wsUrl});
    }
    app.webSocket.sendWebSocketMsg({
      data: {
        "logId": logId,
        "packageMsg": "",
        "packageType": 5,
        "doctorImId": doctorImId
      }
    })
    app.webSocket.onReceivedMsg(res => {
      if (res.packageType === 6) {
        that.logList = $service.deepClone(res.list);
        that.logList.forEach(item => {
          item.patientPhone = item.patientPhone.substring(0,4)+"****"+item.patientPhone.substring(8)
          if (item.logId == logId) {
            inquiryPatientId = item.inquiryPatientId
          }
        })
        this.setData({
          logList: that.logList
        })
      } else if (res.packageType === 16) {
        this.showModal('医生已分诊，当前问诊费会在1-3个工作日原路返回', '16')
      } else if (res.packageType === 23) {
        this.showModal('医生已掉线,请重新问诊', '9')
      } else if (res.packageType === 22) {
        this.showModal(`该医生只支持${log_type == '2' ? '视频' : '图文'}问诊，您可以退诊查费，再重新发起问诊`, '22')
      }
    })
  },
  showModal(content, type) {
    wx.showModal({
      title:'提示',
      content: content,
      confirmText:"确定",
      showCancel: false,
      success:(res) => {
          if (res.confirm) {
            this.cancel()
          }
      }
    }) 
  }

})